// let base_url = process.env.NODE_ENV === 'development' ?  'http://bbsf.test/api/':'https://px.aibaiba.com/api/';
 // let base_url = process.env.NODE_ENV === 'development' ?  'http://bbsf.test/api/':process.env.VUE_APP_BASE_API+'/api/' 
// let base_url = process.env.NODE_ENV === 'development' ?  'https://px.aibaiba.com/api/':'https://px.aibaiba.com/api/' 
let base_url = 'https://px.aibaiba.com/api/'
// let isOver = true
// 封装请求方法
const request = ({
	url,
	method = 'POST',
	data
}) => {
	const token = uni.getStorageSync('token') || ''
	
	return new Promise((resolve, reject) => {
		// 不需要token的api
		let urls = {
			'Login/Login':'1'
		}
		// return 到res层级的api
		let levels = {
			'Login/Login':'1',
			'Px.pxrec/add':'1', 
			'Px.pxrecExt/add':'1',
			'Px.pxrec/singout':'1',
			'Px.pxrec/singin':'1',
			'Clue.student/upavatar':'1',
			'Px.pxrecnew/add':'1',
			'Admin.User/entry':'1',
			'Pb.attainment/add':'1',
			'Admin.User/getuseronlin':'1',
			'Px.pxrecnew/up_inspect':'1',
			'Student.student/add':'1',
			'Clue.contract/add':'1',
			'Finance.pay/add':'1',
			'Px.faq/detail':'1',
			"Px.faqExt/add":'1',
			'Px.faq/add':'1'
		}
		const param = {
			/*APP*/
			// #ifdef APP
			platform:'APP',
			// #endif
			// #ifdef H5
			platform:'H5',
			// #endif
			// // #ifdef MP-WEIXIN 
			// platform:'MP-WEIXIN',
			// // #endif
		}
		const header = {
			'Content-Type': 'application/json; charset=utf-8', // 设置请求头
		}
		if(!urls[url]){
			let info = uni.getStorageSync('userInfo')
			param['token'] = token
			param['user_id'] = info.user_id
			param['dept_id'] = info.dept_id
			param['comp_no'] = info.comp_no
			param['role_id'] = info.role_id
			
			
			
			// header['token'] = token
			// header['user_id'] = info.user_id
			// header['dept_id'] = info.dept_id
			// header['comp_no'] = info.comp_no
			// header['role_id'] = info.role_id
			
		}
		
		Object.assign(param, data)
		uni.request({
			url: base_url + url,
			method: method,
			data: param,
			header: header,
			success: (res) => {
				if (res.data.event == 100||res.data.status == 200) {
					// uni.hideLoading()
					if(levels[url]){
						resolve(res);
						
					}else{
						resolve(res.data);
					}
				}
				/**if (res.data.event == 102) {
					// if(uni.getStorageSync('loginLoadingShow')) return
					// uni.hideLoading()
					if (isOver) {
						isOver = false
						uni.showToast({
							title: res.data.msg,
							duration: 2000,
							icon: 'none',
							success: () => {
								uni.clearStorageSync()
								uni.setStorageSync('downloadDataShow', true)
								uni.setStorageSync('isFirstLogin', 1)
								uni.setStorageSync('loginLoadingShow', 1)
								setTimeout(() => {
									isOver = true
									uni.reLaunch({
										url: '/pages/login/loginNew'
									})
								}, 2000)
							}
						})
					}
				}  else if (res.data.event == 999) {
					uni.showModal({
						title: '提示',
						content: res.data.msg,
						cancelText: '继续浏览',
						confirmText: '去登录',
						success: res => {
							if (res.confirm) {
								uni.reLaunch({
									url: '/pages/login/loginNew'
								})
							}
						}
					})
				} **/
				else {
					uni.hideLoading()
					uni.showToast({
						title:res.data.msg,
						icon:'none'
					})
					resolve(res.data);
				}
			},
			fail: (err) => {
				uni.hideLoading()
				reject(err);
			},
		});
	});
}
const upload = ({
	url,
	method = 'POST',
	data
}) => {
	const token = uni.getStorageSync('token') || ''
	if(!token) {
		uni.showModal({
			title: '提示',
			content: '您还未登录！',
			cancelText: '关闭',
			confirmText: '去登录',
			success: res => {
				if (res.confirm) {
					uni.reLaunch({
						url: '/pages/login/login'
					})
				}else{
					uni.reLaunch({
						url: '/pages/index/index'
					})
				}
			}
		})
	}
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url, 
			header:{
				'token':token,
				// 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', // 设置请求头
			},
			// files: [{
			// 		name: 'file',
			// 		uri: data
			// 	},
			// 	{
			// 		name: 'thumb',
			// 		uri: data
			// 	},
			// ],
			name: 'file',
			formData: {
				file_:'data',
				// type:'typetypetypetypetype'
			},
			success: (res) => {
				resolve(JSON.parse(res.data))
			}
		});
	})
}

// 配置请求拦截器
uni.addInterceptor('request', {
	// 在发送请求之前做一些处理
	config(requestConfig) {
		// 添加请求头、身份验证等
		// requestConfig.header.Authorization = 'Bearer ' + uni.getStorageSync('token');
		return requestConfig;
	},
	// 请求发生错误时的处理
	fail(error) {
		console.error('请求失败:', error);
	},
});
// type == 1 为普通请求 2为上传
const req = (url, data, method = 'POST', type = 1) => {
	return new Promise((r,v)=>{
		if (type == 1) {
			request({
				url,
				method,
				data,
				
			}).then(res => {
				// console.log(res.data)
				r(res.data)
			})
		} else if (type == 2) {
			upload(base_url+'Upload/upload',method,data).then(res=>{
				r(res)
			})
		}
	})
	
}
export default req;